﻿using JRCore.protocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using WHHXMQ.HXMQutil;
using WHHXMQ.mq;

namespace WHHXMQ
{
    public partial class Service1 : ServiceBase
    {
        public static MQ m_mqClient = null;
        public string gMQUri = null;      //MQurl
        public string gTopicName = null;
        public string gFTPPath = null;
        public string gFTPfolder = null;

        public static PicCenterPush push = null;
        public Service1()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            gMQUri = ConfigurationManager.AppSettings.Get("MQUri");      //MQurl
            gTopicName = ConfigurationManager.AppSettings["TopicName"];
            gFTPPath = ConfigurationManager.AppSettings["FTPPath"];
            gFTPfolder = ConfigurationManager.AppSettings["FTPfolder"];
            if (!Directory.Exists(gFTPfolder)) Directory.CreateDirectory(gFTPfolder);

            fileSystemWatcher1.Path = gFTPfolder;

            try
            {
                m_mqClient = new MQ(gMQUri, gTopicName);
                m_mqClient.Start();
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex.Message);

            }
        }

        protected override void OnStart(string[] args)
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞数据上传海信服务启动");

        }

        protected override void OnStop()
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞数据上传海信服务停止");

        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            string filename = System.IO.Path.GetFileNameWithoutExtension(e.FullPath);//文件名  
            string[] s = filename.Split(new char[] { '-' });

            DataRecvRecord record = new DataRecvRecord();
            FullDataRecvRecord(record, s, e);
            PushHXMQ();
        }

        private void PushHXMQ()
        {
            try
            {

                String strMqData = "";
                if (push.P5 == "2")
                {
                    //strMqData = TDUtil.MakeMqMsg_VIO(push);
                }
                else
                {
                    strMqData = TDUtil.MakeMqMsg_KK(push);
                }

                if (m_mqClient.SendMQMessage(strMqData, "company", "jari"))
                {
                    LogHelper.WriteLog(typeof(Service1), "MQ发送海信成功，消息内容:" + push.P23 + "-" + push.P15 + "-" + push.P18);

                }
                else
                {

                    LogHelper.WriteLog(typeof(Service1), "MQ消息发海信送失败:" + push.P23 + "-" + push.P15 + "-" + push.P18);

                }
            }
            catch (Exception ex)
            {

                LogHelper.WriteLog(typeof(Service1), ex.Message);

            }
        }

        private void FullDataRecvRecord(DataRecvRecord record, string[] s, FileSystemEventArgs e)
        {
            record.P15 = s[0];
            switch (s[1])
            {
                case "东向西":
                    record.P16 = "1";
                    break;
                case "南向北":
                    record.P16 = "2";
                    break;
                case "西向东":
                    record.P16 = "3";
                    break;
                case "北向南":
                    record.P16 = "4";
                    break;
                default:
                    record.P16 = "1";
                    break;
            }
            record.P17 = s[2];
            record.P27 = s[3];
            record.P26 = s[4];

            if (s[6].Contains("无牌"))
            {
                record.P23 = "";
            }
            else
            {
                record.P23 = s[6];
            }
            switch (s[7])
            {
                case "大车":
                    record.P21 = "01";
                    break;
                case "小车":
                    record.P21 = "02";
                    break;
                default:
                    record.P21 = "99";
                    break;
            }
            record.P24 = "0";
            if (s[9].Contains("未知"))
            {
                record.P22 = "";
            }
            else
            {
                record.P22 = s[9];
            }
            record.P5 = s[10];
            record.P13 = s[11];

            record.P8 = s[12];
            record.P18 = s[13];
            string ftpName = e.FullPath.Substring(gFTPfolder.Length);
            record.P37 = (gFTPPath + ftpName).Replace(@"\", @"/");
            push = PicCenterPush.Parse(record.P);
            LogHelper.WriteLog(typeof(Service1), push.P37);

        }
    }
}
